﻿using BaiTu.IOCServices;
using BaiTu.IQueueService;
using BaiTu.QueueEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BaiTu.Logging;

namespace BaiTu.QueueLogging
{
    /// <summary>
    /// 日志的处理
    /// </summary>
    internal class QueueLoggingProcessor
    {
        private IQueue queueSrv = null;
        

        public ILogging.ILogger Logger { get; set; }

        internal QueueLoggingProcessor()
        {
            if (queueSrv == null)
            {
                queueSrv = EngineContext.Current.Resolve<IQueue>();
            }
            Logger = NullLogger.Instance;
        }


        public void Process(object state)
        {
            while (true)
            {
                try
                {
                    Message<Framework.Exceptions.BaiTuSerializedException> msg = queueSrv.GetRequestFromClient<Framework.Exceptions.BaiTuSerializedException>();
                    //获取到异常消息
                    if (msg != null)
                    {
                        if (Logger == null)
                        {
                            Logger = EngineContext.Current.Resolve<BaiTuLog4netLogger>();
                        }
                        Logger.Error(msg.Body.Message); ;
                        Console.WriteLine(msg.Body.Message);
                    }
                }
                catch (Exception ex)
                { 
                
                }
            }
        }
    }
}
